package com.JZ.QM.service.impl;

import com.JZ.QM.common.dao.CombinationDetailInfoDao;
import com.JZ.QM.common.dao.CombinationDynamicInfoDao;
import com.JZ.QM.common.dao.CombinationShareDao;
import com.JZ.QM.common.entity.CombinationDetailInfoEntity;
import com.JZ.QM.common.entity.CombinationDynamicInfoEntity;
import com.JZ.QM.common.entity.CombinationShareEntity;
import com.JZ.QM.dto.CombinationBaseInfoDto;
import com.JZ.QM.dto.CombinationProductDto;
import com.JZ.QM.dto.ProductRatioDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;

import com.JZ.QM.common.dao.CombinationBaseInfoDao;
import com.JZ.QM.common.entity.CombinationBaseInfoEntity;
import com.JZ.QM.service.CombinationBaseInfoService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 *
 *
 * @author lyp
 * @email lyp@gmail.com
 * @date 2020-06-14 11:19:36
 */
@Slf4j
@Service("combinationBaseInfoService")
public class CombinationBaseInfoServiceImpl extends ServiceImpl<CombinationBaseInfoDao, CombinationBaseInfoEntity> implements CombinationBaseInfoService {

    @Resource
    CombinationBaseInfoDao combinationBaseInfoDao;

    @Resource
    CombinationShareDao combinationShareDao;

    @Resource
    CombinationDynamicInfoDao combinationDynamicInfoDao;
    @Resource
    CombinationDetailInfoDao combinationDetailInfoDao;


    /**
     * 分页查询
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<CombinationBaseInfoEntity> page = this.page(
                new Query<CombinationBaseInfoEntity>().getPage(params),
                new QueryWrapper<CombinationBaseInfoEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存新建的组合产品
     * @param combinationProduct
     * @return
     */
    @Transactional
    @Override
    public boolean saveCombinationProduct(CombinationProductDto combinationProduct) {
        //组合产品基本信息
        CombinationBaseInfoEntity combinationBaseInfoEntity = new CombinationBaseInfoEntity();
        //组合产品动态信息
        CombinationDynamicInfoEntity combinationDynamicInfoEntity = new CombinationDynamicInfoEntity();
        //新建组合产品基本信息Flag
        int saveCombinationFlag = 1;
        //组合产品的自增主键
        Long comBaseId = Long.valueOf(0);
        //新建成分产品信息Flag
        int saveProductFlag = 1;
        //新建组合产品动态信息
        int saveCombinationDynamicInfoFlag = 1;
        //进行产品code的组装
        String timeUUid = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String comCode = "QM" + timeUUid;

        //进行属性赋值
        BeanUtils.copyProperties(combinationProduct,combinationBaseInfoEntity);
        combinationBaseInfoEntity.setIsUsed(1);
        combinationBaseInfoEntity.setComBaseCode(comCode);

        saveCombinationFlag = combinationBaseInfoDao.insert(combinationBaseInfoEntity);

        //获取自增主键的值
        comBaseId = combinationBaseInfoEntity.getComBaseId();

        //再分别存储各成分产品以及比例
        List<ProductRatioDto> productRatioDtoList = combinationProduct.getProductRatioDtoList();
        for (ProductRatioDto productratiodto:productRatioDtoList){
            CombinationShareEntity combinationShareEntity = new CombinationShareEntity();
            combinationShareEntity.setComBaseId(comBaseId);
            combinationShareEntity.setProTsCode(productratiodto.getProductCode());
            combinationShareEntity.setComShareRadio(productratiodto.getShareRadio());

            saveProductFlag &= combinationShareDao.insert(combinationShareEntity);
        }

        //新建组合动态信息表
        combinationDynamicInfoEntity.setComBaseId(comBaseId);
        combinationDynamicInfoEntity.setCollectNum(0);
        combinationDynamicInfoEntity.setPurchaseNum(0);
        combinationDynamicInfoEntity.setTransactionNum(0);
        combinationDynamicInfoEntity.setVisitNum(0);

        saveCombinationDynamicInfoFlag = combinationDynamicInfoDao.insert(combinationDynamicInfoEntity);

        if (1 == (saveCombinationFlag & saveProductFlag & saveCombinationDynamicInfoFlag)){
            return true;
        }else{
            return false;
        }
    }


    /**
     * 将数据库实体表转换为前端传输的DTO
     * @param combinationBaseInfoEntity
     * @return
     */
    @Override
    public CombinationBaseInfoDto transform(CombinationBaseInfoEntity combinationBaseInfoEntity) throws ParseException {
        //传输对象DTO
        CombinationBaseInfoDto combinationBaseInfoDto = new CombinationBaseInfoDto();

        //传递不变参数
        combinationBaseInfoDto.setComBaseId(combinationBaseInfoEntity.getComBaseId());
        combinationBaseInfoDto.setCounselorId(combinationBaseInfoEntity.getCounselorId());
        combinationBaseInfoDto.setComBaseDesc(combinationBaseInfoEntity.getComBaseDesc());
        combinationBaseInfoDto.setComBaseName(combinationBaseInfoEntity.getComBaseName());
        combinationBaseInfoDto.setConBaseStartMoney(combinationBaseInfoEntity.getConBaseStartMoney());
        combinationBaseInfoDto.setComBaseCode(combinationBaseInfoEntity.getComBaseCode());

        SimpleDateFormat Format = new SimpleDateFormat("yyyy-MM-dd");
        String CreateTime = Format.format(combinationBaseInfoEntity.getGmtCreate());

        combinationBaseInfoDto.setGmtCreate(CreateTime);

        //设置风险等级
        switch (combinationBaseInfoEntity.getComBaseRiskLevel()){
            case 1 :
                combinationBaseInfoDto.setComBaseRiskLevel("谨慎型产品");
                break;
            case 2 :
                combinationBaseInfoDto.setComBaseRiskLevel("稳健型产品");
                break;
            case 3 :
                combinationBaseInfoDto.setComBaseRiskLevel("平衡型产品");
                break;
            case 4 :
                combinationBaseInfoDto.setComBaseRiskLevel("进取型产品");
                break;
            default:
                combinationBaseInfoDto.setComBaseRiskLevel("激进型产品");
                break;
        }

        //设置组合产品类别
        switch (combinationBaseInfoEntity.getComBaseCate()){
            case 1 :
                combinationBaseInfoDto.setComBaseCate("股票型");
                break;
            case 2 :
                combinationBaseInfoDto.setComBaseCate("基金型");
                break;
            case 3 :
                combinationBaseInfoDto.setComBaseCate("混合型");
                break;
        }

        //设置组合状态
        switch (combinationBaseInfoEntity.getIsUsed()){
            case 0 :
                combinationBaseInfoDto.setComBaseStatus("已下架");
                break;
            case 1 :
                combinationBaseInfoDto.setComBaseStatus("已创建");
                break;
            case 2 :
                combinationBaseInfoDto.setComBaseStatus("已上架");
                break;
        }
        return combinationBaseInfoDto;
    }


    /**
     * 修改组合状态
     * @param comBaseId
     * @param combationState
     * @return
     */
    @Override
    public int modifyCombationState(Long comBaseId,int combationState) {
        //组合更新的结果状态
         int combationStateUpdateResult=-1;
        //组合实体
        CombinationBaseInfoEntity combinationBaseInfoEntity = new CombinationBaseInfoEntity();

        //修改组合的上下架状态
        combinationBaseInfoEntity.setIsUsed(combationState);
        combinationBaseInfoEntity.setComBaseId(comBaseId);

        //更新组合的状态，并返回个更新的结果
        combationStateUpdateResult=combinationBaseInfoDao.updateById(combinationBaseInfoEntity);
        return combationStateUpdateResult;

    }

    /**
     * 组合重名检测
     *
     * @param comName
     * @return
     */
    @Override
    public boolean VerifyrepeatedComName(String comName) {
        //查询条件构造器
        QueryWrapper<CombinationBaseInfoEntity> queryWrapper = new QueryWrapper<>();
        //获取对应的参数
        queryWrapper.eq("com_base_name",comName);
        //满足条件的数量
        Integer number = 0;

        number = combinationBaseInfoDao.selectCount(queryWrapper);

        //true无重名，false有重名
        if(0 == number){
            return  true;
        }else{
            return false;
        }

    }

    /**
     * 获取组合产品详细心理列表列表
     * @param riskLevel 风险等级
     * @return
     */
    @Override
    public ArrayList getCombinationList(Long comBaseId,Integer riskLevel) {
        //组合的详细信息列表
        ArrayList<CombinationDetailInfoEntity> res=new ArrayList<>();
        res= (ArrayList<CombinationDetailInfoEntity>) combinationDetailInfoDao.getCombinationDetailINfo(comBaseId,riskLevel);
        return res;

    }

    /**
     * 根据收益率排行获取组合信息列表
     * @return
     */
    @Override
    public ArrayList getCombinationListByIncome() {
        ArrayList<CombinationDetailInfoEntity> res=new ArrayList<>();
        res= (ArrayList<CombinationDetailInfoEntity>) combinationDetailInfoDao.getCombinationDetailByIncome();
        return res;
    }

    /**
     * 根据组合名称查询组合id
     * @param comBaseName
     * @return
     */
    @Override
    public Long getComBaseIdByName(String comBaseName) {
        Long item = Long.valueOf(0);
        //组合实体
        CombinationBaseInfoEntity combinationBaseInfoEntity = new CombinationBaseInfoEntity();

        QueryWrapper<CombinationBaseInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_base_name",comBaseName);

        combinationBaseInfoEntity = combinationBaseInfoDao.selectOne(queryWrapper);

        if(combinationBaseInfoEntity != null){
            return combinationBaseInfoEntity.getComBaseId();
        }else {
            return item;
        }
    }


}